﻿using System.Collections;
using System.Collections.Generic;
using Network;

public class GameManager :Singleton<GameManager> {

    public int RoomID { protected set; get; }

    private uint nextEntityID;
    public uint NextEntityID
    {
        set
        {
            nextEntityID = value;
        }

        get
        {
            return nextEntityID++;
        }
    }

    Dictionary<uint, Logic> allEntities;

    public void Reset(int roomID)
    {
        RoomID = roomID;
        NextEntityID = 1;
        allEntities = new Dictionary<uint, Logic>();
    }

    public void Destroy()
    {
        allEntities = null;
    }

    public Logic GetEntity(uint EntityID)
    {
        if (allEntities == null)
            return null;
        if (allEntities.ContainsKey(EntityID))
            return allEntities[EntityID];
        else
            return null;
    }

    public uint CreateEntity(int EntityNo, int playerID)
    {
        //读表加载prefab
        string prefabPath = "Character/Commando/InGame/Prefab/Commando2";
        uint entityID = NextEntityID;
        gamemanager_create_entity proto = new gamemanager_create_entity();
        proto.entity_id = entityID; proto.prefab_path = prefabPath;proto.player_id = playerID;
        NetManager.Instance.SendGameMessage(OpCode.gamemanager_create_entity, ProtoUtil.SerializeProto(proto));
        
        Logic logic = Logic.InitLogicEntity(entityID, EntityNo);
        allEntities.Add(entityID, logic);

        VisualEntity vEntity = VisualEntityManager.Instance.GetEntity(entityID);
        vEntity.SetLogic(logic);

        return entityID;
    }

    public void DelEntity(uint entityID)
    {
        gamemanager_del_entity proto = new gamemanager_del_entity();
        proto.entity_id = entityID;
        NetManager.Instance.SendGameMessage(OpCode.gamemanager_del_entity, ProtoUtil.SerializeProto(proto));

        Logic logic = GetEntity(entityID);
        if (logic != null)
        {
            logic.Destroy();
            allEntities.Remove(entityID);
        }
    }
}
